﻿#include "unitinfoeditor.h"

UnitInfoEditor::UnitInfoEditor(QWidget *parent):
    QWidget(parent),
    currentUnit(nullptr)
{
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    createWidgets();
    setupLayout();
}

void UnitInfoEditor::setUnit(Unit *unit)
{
    currentUnit = unit;
    loadUnitData();
}

void UnitInfoEditor::onApplyClicked()
{
   if (!currentUnit)
       return;

   // 更新Unit基本信息
   currentUnit->type = typeEdit->text();
   currentUnit->name = nameEdit->text();
   currentUnit->desc = descEdit->toPlainText();
   currentUnit->codeType = codeTypeEdit->text();
   currentUnit->codeValue = codeValueEdit->text();

//   // 更新Constauts参数
//   for (int i = 0; i < currentUnit->constants.size() && i < constantEdits.size(); ++i)
//   {
//       Unit::Constaut &c = currentUnit->constants[i];
//       const ConstautEditGroup &edits = constantEdits[i];

//       c.index = edits.index->text().toInt();
//       c.name = edits.name->text();
//       c.value = edits.value->text();
//       c.unit = edits.unit->text();
//       c.desc = edits.desc->text();
//   }
}

void UnitInfoEditor::createWidgets()
{
    // Unit组控件
    idEdit = new QLineEdit;
    //idEdit->setReadOnly(true);
    typeEdit = new QLineEdit;
    nameEdit = new QLineEdit;
    descEdit = new QTextEdit;
    codeTypeEdit = new QLineEdit;
    codeValueEdit = new QLineEdit;

    // Constauts容器
    constantsContainer = new QWidget;
    constantsLayout = new QVBoxLayout(constantsContainer);
    constantsLayout->setContentsMargins(2, 2, 2, 2);

    // 滚动区域
    QScrollArea *scrollArea = new QScrollArea;
    scrollArea->setWidgetResizable(true);
    scrollArea->setWidget(constantsContainer);
    scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

//    // 按钮
//    applyButton = new QPushButton("更新修改", this);

    // 创建组
    createUnitGroup();
    //createConstautsGroup(scrollArea);
}

void UnitInfoEditor::setupLayout()
{
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setContentsMargins(5, 5, 5, 5);
    mainLayout->setSpacing(8);

    mainLayout->addWidget(unitGroup);
    mainLayout->addWidget(constantsGroup, 1); // 拉伸参数区域
    //mainLayout->addWidget(applyButton, 0, Qt::AlignBottom);
}

void UnitInfoEditor::createUnitGroup()
{
    unitGroup = new QGroupBox("图元属性");
    QFormLayout *form = new QFormLayout;
    form->setContentsMargins(5, 10, 5, 10);
    form->setSpacing(8);

    form->addRow("编号:", idEdit);
    form->addRow("类型:", typeEdit);
    form->addRow("名称:", nameEdit);
    form->addRow("代码类型:", codeTypeEdit);
    form->addRow("代码值:", codeValueEdit);
    form->addRow("描述:", descEdit);

    unitGroup->setLayout(form);
}

void UnitInfoEditor::createConstautsGroup(QWidget *content)
{
    constantsGroup = new QGroupBox("常量列表");
    QVBoxLayout *layout = new QVBoxLayout;
    layout->setContentsMargins(2, 2, 2, 2);
    layout->addWidget(content);
    constantsGroup->setLayout(layout);
}

void UnitInfoEditor::loadUnitData()
{
    // 清空旧数据
    qDeleteAll(constantEditsWidgets);
    constantEdits.clear();
    constantEditsWidgets.clear();

    if (!currentUnit)
        return;

    // 加载基本信息
    idEdit->setText(currentUnit->id);
    typeEdit->setText(currentUnit->type);
    nameEdit->setText(currentUnit->name);
    descEdit->setText(currentUnit->desc);
    codeTypeEdit->setText(currentUnit->codeType);
    codeValueEdit->setText(currentUnit->codeValue);

    // 加载Constauts参数
    for (const Unit::Constaut &c : currentUnit->constants)
        addConstautEditGroup(c);
}

void UnitInfoEditor::addConstautEditGroup(const Unit::Constaut &c)
{
    QWidget *groupWidget = new QWidget;
    QFormLayout *form = new QFormLayout(groupWidget);
    form->setContentsMargins(0, 5, 0, 5);
    form->setSpacing(5);

    ConstautEditGroup edits;
    edits.index = createConstautEdit(QString::number(c.index), form, "索引:");
    edits.name = createConstautEdit(c.name, form, "名称:");
    edits.value = createConstautEdit(c.value, form, "值:");
    edits.unit = createConstautEdit(c.unit, form, "单位:");
    edits.desc = createConstautEdit(c.desc, form, "描述:");

    constantsLayout->addWidget(groupWidget);
    constantEdits.append(edits);
    constantEditsWidgets.append(groupWidget);
}

QLineEdit *UnitInfoEditor::createConstautEdit(const QString &text, QFormLayout *layout, const QString &label)
{
    QLineEdit *edit = new QLineEdit(text);
    layout->addRow(new QLabel(label), edit);
    return edit;
}

